﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig  
// 
// ---------------------------------------------------------------------
#endregion


#region Usings

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using Microsoft.Practices.Unity;
using Microsoft.Surface.Presentation.Controls;
using ScrumTable.UI.View.Util;
using ScrumTable.UI.View.WinTouch.UserControls.View;
using ScrumTable.UI.ViewModel.Data;
using ScrumTable.UI.ViewModel.Controller;
using Application = System.Windows.Application;

#endregion

namespace ScrumTable.UI.View.WinTouch.UserControls.Tool
{
    /// <summary>
    /// Interaction logic for ToolBox.xaml
    /// </summary>
    public partial class ScreenGrid : ScrumUserControl
    {

        #region Decleration

        private Type _lastUsedScreenType;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        public String Screen { get; set; }

        public String MultipleScreen { get; set; }

        [Dependency]
        public AppScreen MainScreen { get; set; }

        [Dependency]
        public IUnityContainer Container { get; set; }

        [Dependency]
        public ScrumViewModelData ScrumData { get; set; }

        /// <summary>
        /// Offset of this grid. 
        /// </summary>
        public Point ScreenOffSet { get; set; }
        

        /// <summary>
        /// DependencyProperty of HideInfo
        /// </summary>
        public static readonly DependencyProperty HideInfoProperty =
          DependencyProperty.Register("HideInfo",
                                      typeof(bool),
                                      typeof(ScreenGrid),
                                      new UIPropertyMetadata(false));
        /// <summary>
        /// Gets or sets if the info is displayed.
        /// </summary>
        /// <value>if the info is visible</value>
        public bool HideInfo
        {
            get { return (bool)GetValue(HideInfoProperty); }
            set { SetValue(HideInfoProperty, value); }
        }


        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="ToolBox"/> class.
        /// </summary>
        public ScreenGrid()
        {
            ScreenOffSet = new Point();
            InitializeComponent();
            DataContext = this;
        }

        #endregion


        #region Methods

        protected override void ReleaseManagedResources()
        {
            DisposeChildren();
            base.ReleaseManagedResources();
        }

        private void DisposeChildren()
        {
            foreach (var children in ControlPlace.Children)
            {
                if (children is IDisposable)
                {
                    ((IDisposable)children).Dispose();
                }
            }
            ControlPlace.Children.Clear();

        }

        public void SetControl(Type type, string args = null)
        {
            DisposeChildren();
            _lastUsedScreenType = type;
            ScrumData.ConfigManager.AddScreenSettings(Screen, type, args);
            if (_lastUsedScreenType != null)
            {
                if (_lastUsedScreenType == typeof(DynamicBoard))
                {
                    var control = (DynamicBoard) Container.Resolve(_lastUsedScreenType);
                    if (args != null)
                    {
                        control.Config = ElementManager.GetTargetConfig(args);
                    }
                    ControlPlace.Children.Add(control);
                }
                if (_lastUsedScreenType == typeof(ReportControl))
                {
                    var control = (ReportControl)Container.Resolve(_lastUsedScreenType);
                    if (args != null)
                    {
                        control.Model.Name = args;
                    }
                    ControlPlace.Children.Add(control);
                }
                else
                {
                    ControlPlace.Children.Add((UIElement)Container.Resolve(_lastUsedScreenType));
                }
            }
        }


        #endregion


        #region Events

       
        private void OnOpenProjectPlanningUserStories(object sender, RoutedEventArgs e)
        {
            ShowWindow( typeof( ProjectPlanningUserStories ), sender);
        }

        private void OnOpenProjectPlanningIterations(object sender, RoutedEventArgs e)
        {
            ShowWindow(typeof(ProjectPlanningIterations), sender);
        }

        private void OnOpenSprintPlanningTask(object sender, RoutedEventArgs e)
        {
            ShowWindow(typeof(SprintPlanningTask), sender);
        }

        private void OnOpenStackedDailyScrumStoryBoard(object sender, RoutedEventArgs e)
        {
            ShowWindow(typeof(StackedDailyScrumStoryBoard), sender);
        }


        private void OnOpenStoryBoard(object sender, RoutedEventArgs e)
        {
            ShowWindow(typeof(DynamicBoard), sender, NavigationTarget.DailyScrumStoryBoard.ToString());
        }



        private void OnOpenTaskBoard(object sender, RoutedEventArgs e)
        {
            ShowWindow(typeof(DynamicBoard), sender, NavigationTarget.DailyScrumTaskBoard.ToString());
        }



        private void OnClose(object sender, RoutedEventArgs e)
        {
            ShowWindow(null, e);
        }


        private void ShowWindow(Type type, object sender, string args=null)
        {
            SetControl(type, args);
        }

        private void OnUndo(object sender, RoutedEventArgs e)
        {
            ScrumData.DataContext.UndoManager.Undo();
        }


        private void OnRedo(object sender, RoutedEventArgs e)
        {
            ScrumData.DataContext.UndoManager.Redo();
        }

        private void OnLogOut(object sender, RoutedEventArgs e)
        {
            ScrumData.Disconnect();
        }

        
        private void OnTeamMemberOpen(object sender, RoutedEventArgs e)
        {
            var x = Container.Resolve<TeamMemberSelector>();
            x.Width = 400;
            x.Height = 200;
            Container.Resolve<AppScreen>().AddScatteredItem(x, GetCentralPoint(), true, true, null, true);
        }


        private void OnGoToOverView(object sender, RoutedEventArgs e)
        {
            Container.Resolve<ScrumController>().GoTo(NavigationTarget.ScrumMap);
        }

        private void OnFilter(object sender, RoutedEventArgs e)
        {
            var x2 = Container.Resolve<FilterBox>();
            Container.Resolve<AppScreen>().AddScatteredItem(x2, GetCentralPoint(), true, false, ((SurfaceButton)sender).ToolTip.ToString(), true);
        }

        private void OnShowUndoRedoHistory(object sender, RoutedEventArgs e)
        {

            var x = Container.Resolve<UndoRedoHistory>();
            Container.Resolve<AppScreen>().AddScatteredItem(x, GetCentralPoint(), true, true, null, true);

        }

        private Point GetCentralPoint()
        {
            return PointToScreen(new Point(ScreenOffSet.X + Width/2, ScreenOffSet.Y + Height/2));
        }

        private void OnReportClick(object sender, RoutedEventArgs e)
        {
            ShowWindow(typeof(ReportControl), sender, MultipleScreen);
        }
        #endregion

     
    }
}
